package com.carkeeper.billpay.controller;

import com.carkeeper.billpay.pojo.PayLog;
import com.carkeeper.billpay.service.impl.PayLogServiceImpl;
import com.carkeeper.entity.Result;
import com.carkeeper.entity.StatusCode;
import com.carkeeper.storebill.feign.StoreBillFeign;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/paylog")
@CrossOrigin
public class PayLogController {

    @Autowired
    PayLogServiceImpl payLogService;


    /**
     * 根据订单号查询账单详情
     * @param id
     * @return 账单显示的组合实体
     */
    @GetMapping("/findBillCode/{id}")
    Result<Map> findBillCode(@PathVariable("id") String id){

        Integer payId = new Integer(id);


        return payLogService.findBillCode(payId);
    }

    /**
     * 已支付的支付单查询
     * @param searchMap
     * @return
     */
    @GetMapping("/findPayLogListByCondition")
    public Result<List> findPayLogListByCondition(@RequestParam Map<String, String> searchMap){

        PageInfo<PayLog> info = payLogService.findPayLogListByCondition(searchMap);

        return new Result<>(true, StatusCode.OK,"查询成功",info);
    }

    @PostMapping("/payStoreBill")
    public Result payStoreBill(@RequestBody Map map){

        if (map == null || map.size() == 0){

            return new Result<>(false, StatusCode.ERROR,"支付失败");
        }

        ArrayList<Map> billList = (ArrayList<Map>) map.get("StoreBillList");
        if ( billList == null || billList.size() == 0){
            return new Result<>(false, StatusCode.ERROR,"支付失败");
        }

        for (Map bill : billList) {

            PayLog payLog = new PayLog();
            payLog.setCode((String) bill.get("code"));
            payLog.setStoreId(new Integer((String)map.get("storeId")));
            payLog.setPayMoney(Integer.parseInt((String) bill.get("payMoney")));
            payLog.setPayType((String) map.get("payType"));
            payLogService.addPayLog(payLog);

        }

        return new Result<>(true, StatusCode.OK,"支付成功");
    }


    /**
     * 根据订单时间查询当日支付多少钱
     * @param date
     * @return
     */
    @GetMapping("/findTotalMoneyByDate/{date}")
    public Result<Integer> findTotalMoneyByDate(@PathVariable("date") String date){

        int totalMoney = payLogService.findTotalMoneyByDate(date);

        return new Result<>(true,StatusCode.OK,"查询成功",totalMoney);
    }

}
